package com.zhxh.nzpos.sell;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ListView;

import com.zhxh.nzpos.R;
import com.zhxh.nzpos.dao.CategoryDao;
import com.zhxh.nzpos.model.Category;
import com.zhxh.nzpos.setting.CateItemActionCallback;
import com.zhxh.nzpos.setting.PriceCategoryAdapter;

/**
 * 商品销售-选择商品-
 * @author Administrator
 *
 */
public class TitlesFragment extends ListFragment implements CateItemActionCallback{
	private CategoryDao categoryDao;
	ArrayList<Category> categorys;
	
	List<Category> leveltop;
	
	public String parentId;
	
    public static TitlesFragment newInstance(String cateid) {
    	
    	TitlesFragment f = new TitlesFragment();
        f.parentId = cateid;

        return f;
    }
    
    @Override
    public void onActivityCreated(Bundle savedState) {
        super.onActivityCreated(savedState);
        
        //显示分类
		categoryDao = new CategoryDao(getActivity());
        if(parentId == null) {
        	leveltop = categoryDao.getAllParents();
        	if(leveltop.size() > 0) {
        		parentId = leveltop.get(0).getId();
        	}
        }
		categorys = categoryDao.getAllChildren(parentId);
        setListAdapter(new PriceCategoryAdapter(getActivity().getLayoutInflater(),  categorys,this));
        
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
      //  showDetails(checkedIndex);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        //outState.putInt("checkedIndex", checkedIndex);
    }

    @Override
    public void onListItemClick(ListView l, View v, int pos, long id) {
      //  showDetails(pos);
    }

    void showDetails(String cateid) {

        View detailsFrame = getActivity().findViewById(R.id.goodslist);
        if (detailsFrame != null) {
        	DetailsFragment details = (DetailsFragment)getFragmentManager().findFragmentById(R.id.goodslist);
        	if (details == null || !cateid.equals(details.parentId)) {
        		details = DetailsFragment.newInstance(cateid);

        		FragmentTransaction ft = getFragmentManager().beginTransaction();
        		ft.replace(R.id.goodslist, details);
        		ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        		ft.commit();
        	}
        } 
    }

	@Override
	public void doSelect(String cateid) {
		showDetails(cateid);
	}
}
